<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="课程名字" prop="courseName">
        <el-input
          v-model="dataForm.courseName"
          placeholder="课程名字"
        ></el-input>
      </el-form-item>
      <el-form-item label="课程封面" prop="courseHover">
        <el-input
          v-model="dataForm.courseHover"
          placeholder="课程封面"
        ></el-input>
      </el-form-item>
      <el-form-item label="课程简介(不超过100个字)" prop="courseDescription">
        <el-input
          v-model="dataForm.courseDescription"
          placeholder="课程简介(不超过100个字)"
        ></el-input>
      </el-form-item>
      <el-form-item label="课程类型" prop="courseTypeId">
        <el-input
          v-model="dataForm.courseTypeId"
          placeholder="课程类型"
        ></el-input>
      </el-form-item>
      <el-form-item label="课程讲师" prop="teacherId">
        <el-input
          v-model="dataForm.teacherId"
          placeholder="课程讲师"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        courseName: "",
        courseHover: "",
        courseDescription: "",
        courseTypeId: "",
        teacherId: "",
        createTime: "",
        updateTime: "",
        isDelete: "",
      },
      dataRule: {
        courseName: [
          { required: true, message: "课程名字不能为空", trigger: "blur" },
        ],
        courseHover: [
          { required: true, message: "课程封面不能为空", trigger: "blur" },
        ],
        courseDescription: [
          {
            required: true,
            message: "课程简介(不超过100个字)不能为空",
            trigger: "blur",
          },
        ],
        courseTypeId: [
          { required: true, message: "课程类型不能为空", trigger: "blur" },
        ],
        teacherId: [
          { required: true, message: "课程讲师不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: false, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: false, message: "修改时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/course/course/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.courseName = data.course.courseName;
              this.dataForm.courseHover = data.course.courseHover;
              this.dataForm.courseDescription = data.course.courseDescription;
              this.dataForm.courseTypeId = data.course.courseTypeId;
              this.dataForm.teacherId = data.course.teacherId;
              this.dataForm.createTime = data.course.createTime;
              this.dataForm.updateTime = data.course.updateTime;
              this.dataForm.isDelete = data.course.isDelete;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/course/course/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              courseName: this.dataForm.courseName,
              courseHover: this.dataForm.courseHover,
              courseDescription: this.dataForm.courseDescription,
              courseTypeId: this.dataForm.courseTypeId,
              teacherId: this.dataForm.teacherId,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              isDelete: this.dataForm.isDelete,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
